var rootURL=getRootPath();
var course={
    id:null,
    week: null,
    lessonsId: null,
    grade: '1',
    classs: '1',
    context: null,
    name: null,
    haveClass: null,
    classOver: null,
    lessonsNo: null,
    standby1: null,
    standby2: null,
    standby3: null,
    standby4: null,
    standby5: null,
    standby6: null
};
var ids=[];
var state=0;//1新增，2修改


$(document).ready(function(){
    selcourse(course);
    selGrade(1);
    selClasss('1',1);
    clearmodel();
    $('#addGCWindow').hide();
    $("#grade1").change(function () {
        course.grade=null;
        var options=$("#grade1 option:selected");
        var grade=options.val();
        course.grade=grade;
        selClasss(grade,1);
        selcourse(course);
    });
    $("#classs1").change(function () {
        course.classs=null;
        var options=$("#classs1 option:selected");
        var classs=options.val();
        course.classs=classs;
        selcourse(course);
    });
    $('#addclass').click(function(){
        clearmodel();
        state=1;
        selGrade(2);
        selClasss('1',2);
    });
    $("#grade2").change(function () {
        course.grade=null;
        var options=$("#grade2 option:selected");
        var grade=options.val();
        course.grade=grade;
        selClasss(grade,2);
        selcourse(course);
    });
    $("#classs2").change(function () {
        course.classs=null;
        var options=$("#classs2 option:selected");
        var classs=options.val();
        course.classs=classs;
        selcourse(course);
    });
    $('#save').click(function(){
        var options=$("#week option:selected");
        var week=options.val();
        course.week=week;
        var options=$("#lessonsNo option:selected");
        var lessonsNo=options.val();
        course.lessonsNo=lessonsNo;
        course.lessonsId=$('#lessonsId').val();
        var options=$("#grade2 option:selected");
        var grade=options.val();
        course.grade=grade;
        var options=$("#classs2 option:selected");
        var classs=options.val();
        course.classs=classs;
        course.name=$('#name').val();
        addClasss(course);

    });
    $('#addGC').click(function(){
        $('#addGCWindow').show();
        $('#addG').val('');
        $('#addC').val('');
    });
    $('#add').click(function(){
        addGC();
    });
    $('#delclass').click(function () {
        if(confirm("确定删除"+course.grade+"年级"+course.classs+"班的课程表？")){
            delAll(course);
        }else{
            alert("取消操作!");
        }
    });
});

//查询课程表
function selcourse(course){
    $.ajax({
        url:rootURL+"selCourse",
        type:"get",
        async:true,
        data:{
            grade: course.grade,
            classs: course.classs
        },
        success:function(classtable){
            $('#classtable tbody td').empty();
            $('#td1').append(1);
            $('#td2').append(2);
            $('#td3').append(3);
            $('#td4').append(4);
            $('#td5').append(5);
            $('#td6').append(6);
            $('#td7').append(7);
            $('#td8').append(8);
            $('#td9').append(9);
            $('#td10').append(10);
            var course = classtable.extend.course;
            $.each(course, function(index, item) {
                ids.push(item.id);
                var updbtn=$('<button data-toggle="modal" data-target="#myModal" class="btn btn-default btn-xs upd-btn" upd-id="'+item.id+'"><span class="glyphicon glyphicon-pencil"></span></button>');
                var C=$('<div class="course"></div>').append(item.lessonsId);
                var delbtn=$('<button class="btn btn-default btn-xs del-btn" del-id="'+item.id+'"><span class="glyphicon glyphicon-trash"></span></button>');
                var cou=$('<div style="margin: 0 auto"></div>').append(updbtn).append(C).append(delbtn);
                $('#td'+item.lessonsNo+item.week).append(cou);
            });
        }
    });
}

//清空模态框
function clearmodel() {
    selGrade(2);
    selClasss('1',2);
    $('#addG').val('');
    $('#addC').val('');
    $('#name').val('');
    $('#lessonsId').val('');
}

// 删除按钮
$(document).on("click", ".del-btn", function() {
    var id = $(this).attr("del-id");
    if (confirm("确认删除吗？")) {
        $.ajax({
            url : rootURL+"delCou",
            type : "get",
            data:{
                id:id
            },
            success : function(result) {
                alert(result.msg);
                selcourse(course);
            }
        });
    }
});
// 修改按钮
$(document).on("click", ".upd-btn", function() {
    clearmodel();
    state=2;
    var id = $(this).attr("upd-id");
    course.id=id;
    $.ajax({
        url:rootURL+"selCourse",
        type:"get",
        async:true,
        data:{
            id:id
        },
        success:function(classtable){
            var course = classtable.extend.course;
            $.each(course, function(index, item) {
                $('#week').val(item.week);
                $('#lessonsNo').val(item.lessonsNo);
                $('#grade2').val(item.grade);
                $('#classs2').val(item.classs);
                $('#name').val(item.name);
                $('#lessonsId').val(item.lessonsId);
                console.info(id);
            });
        }
    });
});

//查询年级
function selGrade(e){
    $.ajax({
        url:rootURL+"selG",
        type:"get",
        async:true,
        success:function(Grade){
            $('#grade'+e).empty();
            var grade = Grade.extend.grade;
            $.each(grade, function(index, item) {
                $('<option value='+item.grade+'></option>').append(item.grade+'年级').appendTo('#grade'+e);
            });
        }
    });
}

//查询班级
function selClasss(grade,e){
    $.ajax({
        url:rootURL+"selC",
        type:"get",
        async:true,
        data:{
            grade:grade
        },
        success:function(Classs){
            $('#classs'+e).empty();
            var classs = Classs.extend.classs;
            $.each(classs, function(index, item) {
                $('<option value='+item.classs+' cla-id='+item.id+'></option>').append(item.classs+'班').appendTo('#classs'+e);
            });
        }
    });
}

//添加/修改课程
function addClasss(course){
    var url=null;
    if(state==1){
        url=rootURL+"addCourse";
    }else if(state==2){
        url=rootURL+"updCourse";
    }
    $.ajax({
        url:url,
        type:"get",
        async:true,
        data:{
            id:course.id,
            week:course.week,
            lessonsNo:course.lessonsNo,
            lessonsId:course.lessonsId,
            grade:course.grade,
            classs:course.classs,
            name:course.name
        },
        success:function(result){
            code=result.code;
            if(code==100){
                course.id=null;
                selcourse(course);
                $('#grade1').val(course.grade);
                $('#classs1').val(course.classs);
                clearmodel();
            }else{
                if(state==1){
                    alert("添加失败!");
                }else if(state==2){
                    alert("修改失败!");
                }
            }
        }
    });
}

//添加班级
function addGC(){
    $.ajax({
        url:rootURL+"addGC",
        type:"get",
        async:true,
        data:{
            grade:$('#addG').val(),
            classs:$('#addC').val()
        },
        success:function(result){
            code=result.code;
            if(code==100){
                selGrade(2);
                selClasss($('#addG').val(),2);
                $('#addGCWindow').hide();
            }else{
                alert("添加失败!");
            }
        }
    });
}

//删除年级
function delG(grade){
    $.ajax({
        url:rootURL+"delG",
        type:"get",
        async:true,
        data:{
            grade:"",
        },
        success:function(result){
            code=result.code;
            if(code==100){
                selGrade(2);
            }else{
                alert("删除失败!");
            }
        }
    });
}

//删除班级
function delC(id){
    $.ajax({
        url:rootURL+"delC",
        type:"get",
        async:true,
        da:{            id:""
        },
        success:function(result){
            code=result.code;
            if(code==100){
                selGrade(2);
            }else{
                alert("删除失败!");
            }
        }
    });
}

//删除课表
function delAll(course) {
    var options=$("#classs1 option:selected");
    var claid=options.attr('cla-id');
    $.ajax({
        url:rootURL+"delAll",
        type:"get",
        async:true,
        data:{
            claid:claid,
            grade:course.grade,
            classs:course.classs
        },
        success:function(result){
            code=result.code;
            if(code==100){
                selcourse(course);
            }else{
                alert("删除失败!");
            }
        }
    });
}